<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑页</title>
    <link rel="stylesheet" href="./css/normal.css">
    <link rel="stylesheet" href="./css/edit.css">
    <link rel="stylesheet" href="editormd/css/editormd.css" />
</head>

<body>
    <div class="nav">
        <img src="./pic/dog.png" alt="">
        <span class="blog_title">我的博客系统</span>
        <span class="space"></span>
        <a href="./blog_list.html" class="nav_span">主页</a>
        <a href="#" class="nav_span">写博客</a>
        <a href="reg.html" class="nav_span">注册</a>
        <a onclick="logout()" class="nav_span">注销</a>

    </div>
    <div class="content-edit">

            <div class="push">
                <input type="hidden" id="blogId">
                <input type="text" name="title" id="title">
                <input type="submit" value="更新文章" id="btn" onclick="updateBlog()">
            </div>
            <div class="editor">
                <div id="test-editor">
                    <textarea style="display:none;" name="content" id="content">##开始写你的博客吧~~~</textarea>
                </div>
            </div>

    </div>

    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="editormd/editormd.min.js"></script>
    <script src="./js/common.js"></script>

    <script type="text/javascript">

        $(function () {
            var editor = editormd("test-editor", {
                width: "100%",
                height: "100%",
                path: "editormd/lib/"
            });
        });
       //获取博客信息, 显示在页面上
       function getBlogInfo(){
            $.ajax({
                type:"get",
                url:"/blog/getblog"+location.search,
                success:function(result){
                    if(result.code==200 && result.data!=null){
                        //把博客信息, 显示在页面上
                        var blog = result.data;
                        $("#title").val(blog.title);
                        $("#content").val(blog.content);
                        $("#blogId").val(blog.id);
                    }
                },
                error:function(error){
                    if(error!=null && error.status==401){
                        alter("用户未登录");
                        location.href="blog_login.html";
                    }
                }
            });
        }
        getBlogInfo();
        function updateBlog(){
            $.ajax({
                type:"post",
                url:"/blog/update",
                data:{
                    "title": $("#title").val(),
                    "content":$("#content").val(),
                    "id":$("#blogId").val()
                },
                success:function(result){
                    if(result.code==200 && result.data==true){
                        alert("博客更新成功");
                        location.href = "blog_list.html"
                    }
                }
            });
        }
        var url = "/user/getlogininfo";
        getUserInfo(url);
    </script>
</body>

</html>